# ! /usr/bin/nev python
# -*-coding:utf-8*-
#author:Big Big
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from data_see import *
import datetime
#评论数与评分的联系的分析
def mark_contact(df):
    mark_people = df[['人数','评分']]
    #绘制蜂巢图
    df_this = pd.DataFrame({'mark': mark_people['评分'].values,
                       'people': mark_people['人数'].values})
    sns.jointplot(x='mark', y='people', data=df_this, kind='hex')
    plt.title('contact between mark and people')
    plt.savefig(fname="mark_con.png",figsize=[10,10])
    plt.show()

#评论数与电影片长的联系分析
def time_contact(df):
    time_prople = df[['人数','片长']]
    #绘制散点图
    df_this = pd.DataFrame({'time': time_prople['片长'].values,
                            'people': time_prople['人数'].values})
    sns.jointplot(x='time', y='people', data=df_this, kind='scatter')
    plt.title('contact between filmtime and people')
    plt.savefig(fname="time_con.png",figsize=[10,10])
    plt.show()
#评论数与国籍的联系分析
def country_contack(df):
    allcountry,uesless = get_allcountry_message(df)
    allcountry_english = [' us', 'China', 'Hong Kong', 'Mexico', 'Australia', 'Canada', 'French', 'Italian', 'Japan', 'uk', 'India', 'Switzerland', 'German', 'Korea', 'New Zealand', 'Lebanon','Cyprus', 'Qatar', 'Taiwan', 'polish', 'Spain', 'Iran', 'Denmark', 'Sweden', 'Brazil', 'Austria', 'South Africa', 'Greece', 'Argentina', 'Thailand', 'Ireland,','Hungary', 'Jordan', 'the Czech', 'the united Arab emirates', 'Monaco ',' Belgium ']
    #记录每个国家的评论数
    count = []
    for i in range(len(allcountry_english)):
        count.append(0)
        data = dict((zip(allcountry_english,count)))
    for index,country in enumerate(df['国籍'].values):
        country = re.findall('[\u4e00-\u9fa5]+',country)
        for i in country:
            index_english = allcountry.index(i)
            data[allcountry_english[index_english]] += df['人数'].values[index]

    last = list(zip(data.values(),data.keys()))
    last_sorted = sorted(last,reverse=True)
    x = []
    y = []
    for i,l in last_sorted:
        x.append(l)
        y.append(i)
    plt.bar(x[0:10], y[0:10], color='b', width=0.25, label="amount")
    plt.title('contact between countrt and people')
    plt.savefig(fname="country_con.png",figsize=[10,10])
    plt.show()
#评论数与电影类型之间
def type_contact(df):
    # 获取到所有类型后翻译成英文
    alltypes_english = ['drama', 'crime', 'love', 'gay', 'disaster', 'action', 'comedy', 'war', 'animated', 'fantasy',
                        'history', 'science', 'mystery', 'adventure', 'music', 'dancing', 'thriller', 'ancient costume',
                        'family', 'biography', 'Sport ', ' Western ', 'Erotica ', ' Children ', 'Documentary ',
                        ' Martial Arts ', 'Horror ']
    alltypes = []
    # 数据清洗
    for i in list(df['类型'].values):
        typenames = re.findall('[\u4e00-\u9fa5]+', i[1:-1])
        for type in typenames:
            if type not in alltypes:
                alltypes.append(type)
    #记录每个类型的评论数量
    count = []
    for i in range(len(alltypes)):
        count.append(0)
    data = dict((zip(alltypes_english, count)))
    for index,types in enumerate(df['类型'].values):
        types = re.findall('[\u4e00-\u9fa5]+',types)
        for i in types:
            index_english = alltypes.index(i)
            data[alltypes_english[index_english]] += df['人数'].values[index]

    last = list(zip(data.values(), data.keys()))
    last_sorted = sorted(last, reverse=True)
    x = []
    y = []
    for i, l in last_sorted:
        x.append(l)
        y.append(i)
    plt.bar(x[0:10], y[0:10], color='b', width=0.25, label="amount")
    plt.title('contact between filmtype and people')
    plt.savefig(fname="type_con.png",figsize=[10,10])
    plt.show()
#评论数量与电影年份的关系
def year_contact(df):
    #数据清洗
    data = df.loc[:, ['年份', '人数']]  # 获取数据集中列名为年份和人数这两列
    base = data.sort_values(by='年份')  # 按年份这一列进行排
    x = base['年份'].values
    y = base['人数'].values
    plt.plot(x,  # x轴数据
             y,  # y轴数据
             linestyle='-',  # 折线类型
             linewidth=2,  # 折线宽度
             color='steelblue',  # 折线颜色
             )
    plt.ylabel('amount')
    plt.xlabel('1931-2021')
    plt.title('the contact between year and people')
    # 显示图形
    plt.savefig(fname="year_con.png", figsize=[10, 10])
    plt.show()



